package com.yxym.generic;

import java.util.ArrayList;

@SuppressWarnings({"all"})
public class Generic {
    public static void main(String[] args) {

        //添加对象
//        ArrayList arrayList = new ArrayList();

        //改进
        ArrayList<Dog> arrayList = new  ArrayList<Dog>();

        arrayList.add(new Dog("旺财",1));
        arrayList.add(new Dog("小何",5));
        arrayList.add(new Dog("次奥佰",2));

        //arrayList.add(new Cat("而是人",3));
        //编译器发现添加的类型不满足要求，会报错

        //遍历
        for (Dog dog :arrayList) {
//            //向下转型Object->Dog
//            Dog dog = (Dog) o;
            System.out.println(dog.getName() + "---" +dog.getAge());
        }
    }
}

class Dog{
    private String name;
    private int age;
    public Dog(String name , int age){
        this.age = age;
        this.name = name;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Cat{
    private String name;
    private int age;
    public Cat(String name , int age){
        this.age = age;
        this.name = name;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}